home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dave falkenburg's sprocket / lib / appleeventhandling.cp next >
Encoding:
Text File  |  2000-09-28  |  10.3 KB  |  343 lines

  1. /*
  2.     File:        AppleEventHandling.cp
  3.  
  4.     Contains:    Minimalist support for the required and Display Manager AppleEvents
  5.                 We also support openning and printing “LetterSpec” documents for AOCE.
  6.                 
  7.                 To really support AppleScript™, we’ll need to do ALOT more work in here.
  8.  
  9.     Written by:     
  10.  
  11.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  12.  
  13.                 You may incorporate this Apple sample source code into your program(s) without
  14.                 restriction. This Apple sample source code has been provided "AS IS" and the
  15.                 responsibility for its operation is yours. You are not permitted to redistribute
  16.                 this Apple sample source code as "Apple sample source code" after having made
  17.                 changes. If you're going to re-distribute the source, we require that you make
  18.                 it clear in the source that the code was descended from Apple sample source
  19.                 code, but that you've made changes.
  20.  
  21.     Change History (most recent first):
  22.                 8/19/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  23.                 12/15/94    DRF                Added ChooseApplicationAsAETarget.
  24.                 11/23/94    DRF                InstallAppleEventHandlers is now called InitAppleEventRoutines.
  25.                                             Also added a useful global: gThisProcessDesc for self targetting
  26.                                             of AppleEvents.
  27.                 11/16/94    DRF                Add StandardAEIdleProc for people who like using AESend with
  28.                                             kAEWaitReply.
  29.                 11/12/94    DRF                Removed extra #include.
  30.                  9/27/94        DRF                 AppLib.h is now Sprocket.h
  31.                 9/4/94        DRF                Added stub Text Services AppleEvent handlers.
  32.  
  33. */
  34.  
  35. #include "Sprocket.h"
  36.  
  37. #include <AppleEvents.h>
  38. #include <Errors.h>
  39. #include <Displays.h>            //    for Display Manager AppleEvent constants
  40. #include <OCEStandardMail.h>    //    for LetterSpec
  41.  
  42. #if    qInlineInputAware
  43. #include <TextServices.h>
  44. #endif
  45.  
  46. #include "AppleEventHandling.h"
  47. #include <PPCToolbox.h>
  48. #include <EPPC.h>
  49.  
  50. static pascal Boolean StandardAEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn);
  51.     
  52.  
  53. AEDesc        gThisProcessDesc;
  54.  
  55. void
  56. InitAppleEventRoutines(void)
  57.     {
  58.     ProcessSerialNumber    thisProcess;
  59.  
  60.     //    Create useful AE Targets for our aplication
  61.  
  62.     //    By building the target in this fashion, we get the best performance
  63.     //    out of the AppleEvent manager because it can call our event handlers
  64.     //    inline.
  65.  
  66.     thisProcess.highLongOfPSN     = 0;
  67.     thisProcess.lowLongOfPSN     = kCurrentProcess;
  68.     (void) AECreateDesc(typeProcessSerialNumber,(Ptr)&thisProcess, sizeof(ProcessSerialNumber),&gThisProcessDesc);
  69.  
  70.  
  71.     //    It’s probably more efficient to use a table to install these handlers…
  72.     
  73.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,NewAEEventHandlerProc(HandleOpenApplication),0,false);
  74.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,NewAEEventHandlerProc(HandleOpenDocuments),0,false);
  75.     (void) AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,NewAEEventHandlerProc(HandlePrintDocuments),0,false);
  76.     (void) AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,NewAEEventHandlerProc(HandleQuitApplication),0,false);
  77.  
  78.     //    regardless of whether or not we have the display manager, go ahead and register an AppleEvent handler
  79.     (void) AEInstallEventHandler(kCoreEventClass,kAESystemConfigNotice,NewAEEventHandlerProc(HandleSystemConfigNotice),0,false);
  80.  
  81. #if    qInlineInputAware
  82.     if (gHasTextServices)
  83.         {
  84.         (void) AEInstallEventHandler(kTextServiceClass,kUpdateActiveInputArea,NewAEEventHandlerProc(HandleTextServicesUpdateActiveInputArea),0,false);
  85.         (void) AEInstallEventHandler(kTextServiceClass,kPos2Offset,NewAEEventHandlerProc(HandleTextServicesPos2Offset),0,false);
  86.         (void) AEInstallEventHandler(kTextServiceClass,kOffset2Pos,NewAEEventHandlerProc(HandleTextServicesOffset2Pos),0,false);
  87.         //    we don’t need to handle <kTextServiceClass,kShowHideInputWindow> events
  88.         }
  89. #endif
  90.     }
  91.  
  92.  
  93. OSErr
  94. CheckAppleEventForMissingParams(AppleEvent *theAppleEvent)
  95.     {
  96.     DescType    returnedType;
  97.     Size        actualSize;
  98.     OSErr        err;
  99.     
  100.     err = AEGetAttributePtr(theAppleEvent,keyMissedKeywordAttr,typeWildCard,
  101.                             &returnedType,nil,0,&actualSize);
  102.     
  103.     if (err == errAEDescNotFound)        //    If we couldn’t find the error attribute
  104.         return noErr;                    //        everything is ok, return noErr
  105.     
  106.     if (err == noErr)                    //    We found an error attribute, so
  107.         return errAEEventNotHandled;    //        tell the client we ignored the event
  108.  
  109.     return err;                            //    Something else happened, return it
  110.     }
  111.  
  112.  
  113. OSErr
  114. ChooseApplicationAsAETarget(AEAddressDesc *targetDesc, StringPtr prompt)
  115.     {
  116.     PortInfoRec        thePortInfo;
  117.     LocationNameRec    theLocation;
  118.     TargetID        theTargetID;
  119.     OSErr            err;
  120.  
  121.     err = PPCBrowser(prompt, "\p", false, &theLocation, &thePortInfo, (PPCFilterUPP) NULL, "\p");
  122.     if (err == noErr)
  123.         {
  124.         theTargetID.name = thePortInfo.name;
  125.         theTargetID.location = theLocation;
  126.  
  127.         err = AECreateDesc(typeTargetID,&theTargetID,sizeof(theTargetID),targetDesc);
  128.         }
  129.  
  130.     return err;
  131.     }
  132.  
  133.  
  134. AEIdleUPP        StandardAEIdleUPP = NewAEIdleProc(StandardAEIdleProc);
  135.  
  136. static pascal Boolean
  137. StandardAEIdleProc(EventRecord *theEvent, long * /* sleepTime */, RgnHandle * /* mouseRgn */)
  138.     {
  139.     HandleEvent(theEvent);            //    First, always hand event off to our event handling code
  140.     return false;                    
  141.     }
  142.  
  143.  
  144. OSErr
  145. ForEachDocumentInList(AEDescList documentList,EachDocumentProcPtr documentProc,void * documentParam)
  146.     {
  147.     long                documentCount,documentIndex;
  148.     DescType            returnedType;
  149.     Size                actualSize;
  150.     LetterDescriptor    theLetterDesc;
  151.     AEKeyword            keyword;
  152.     OSErr                err;
  153.  
  154.     if ((err = AECountItems(&documentList,&documentCount)) != noErr)
  155.         return err;
  156.     
  157.     for (documentIndex=1; documentIndex <= documentCount; documentIndex++)
  158.         {
  159.         //    What kind of document is it?
  160.         if ((err = AESizeOfNthItem(&documentList,documentIndex,&returnedType,&actualSize)) != noErr)
  161.             return err;
  162.         
  163.         //    Is it an AOCE letter?
  164.         if (returnedType == typeLetterSpec)
  165.             {
  166.             //    It’s a letter
  167.             theLetterDesc.onDisk = false;
  168.             err = AEGetNthPtr(&documentList,documentIndex,typeLetterSpec,&keyword,&returnedType,
  169.                                 (Ptr) &theLetterDesc.u.mailboxSpec, sizeof(theLetterDesc.u.mailboxSpec),&actualSize);
  170.             }
  171.         else
  172.             {
  173.             //    It’s just a normal document file
  174.             theLetterDesc.onDisk = true;
  175.             err = AEGetNthPtr(&documentList,documentIndex,typeFSS,&keyword,&returnedType,
  176.                                 (Ptr) &theLetterDesc.u.fileSpec,sizeof(theLetterDesc.u.fileSpec),&actualSize);
  177.             }
  178.             
  179.         if (err == noErr)
  180.             (*documentProc)(&theLetterDesc,documentParam);
  181.         else
  182.             break;
  183.         }
  184.     
  185.     return    err;
  186.     }
  187.  
  188.     
  189. pascal OSErr
  190. HandleOpenApplication(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  191.     {
  192.     OSErr    err;
  193.     
  194.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  195.         return err;
  196.  
  197.     return(CreateNewDocument());
  198.     }
  199.  
  200.  
  201. pascal OSErr
  202. HandleOpenDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  203.     {
  204.     AEDescList            documentList;
  205.     OSErr                err;
  206.     
  207.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  208.         return err;
  209.  
  210.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  211.         return err;
  212.  
  213.     err = ForEachDocumentInList(documentList,&OpenDocument,nil);
  214.     (void) AEDisposeDesc(&documentList);
  215.     return err;
  216.     }
  217.  
  218.  
  219. pascal OSErr
  220. HandlePrintDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  221.     {
  222.     AEDescList            documentList;
  223. #if    qUseQuickDrawGX
  224.     AEDescList            desktopPrinterList;
  225.     FSSpec                thePrinterFSSpec;
  226.     Boolean                draggedToDesktopPrinter = false;
  227. #endif
  228.     OSErr                err;
  229.     
  230.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  231.         return err;
  232.  
  233. #if    qUseQuickDrawGX
  234.     if (noErr == AEGetAttributeDesc(theAppleEvent,keyOptionalKeywordAttr,typeAEList,&desktopPrinterList))
  235.         draggedToDesktopPrinter = true;
  236. #endif
  237.  
  238.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  239.         return err;
  240.  
  241. #if    qUseQuickDrawGX
  242.     if (draggedToDesktopPrinter)
  243.         {
  244.         DescType            returnedType;
  245.         Size                actualSize;
  246.         AEKeyword            keyword;
  247.  
  248.         err = AEGetNthPtr(&desktopPrinterList,1,typeFSS,&keyword,&returnedType,
  249.                             (Ptr) &thePrinterFSSpec,sizeof(thePrinterFSSpec),&actualSize);
  250.  
  251.         (void) AEDisposeDesc(&desktopPrinterList);
  252.         }
  253.     err = ForEachDocumentInList(documentList,&PrintDocument,draggedToDesktopPrinter ? &thePrinterFSSpec : NULL);
  254.     (void) AEDisposeDesc(&documentList);
  255. #else
  256.     err = ForEachDocumentInList(documentList,&PrintDocument,nil);
  257. #endif
  258.  
  259.     return err;
  260.     }
  261.  
  262.  
  263. pascal OSErr
  264. HandleQuitApplication(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  265.     {
  266.     OSErr    err;
  267.     
  268.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  269.         return err;
  270.  
  271.     gDone = QuitApplication();
  272.     
  273.     if (gDone)
  274.         return noErr;
  275.     else
  276.         return userCanceledErr;
  277.     }
  278.  
  279.  
  280. ////////////////////////////////////////////////////////////////////////
  281. //
  282. //    Display Manager Suite is “Under Construction”
  283. //
  284. //    To Do: Check ERS for Display Manager events
  285.  
  286. pascal OSErr
  287. HandleSystemConfigNotice(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  288.     {
  289.     AEDescList    displayList;
  290.     long        displayCount,displayIndex;
  291.     OSErr        err;
  292.     
  293.     DebugStr((StringPtr) "\pGot a Display Manager Event!");
  294.     
  295.     if ((err = AEGetParamDesc(theAppleEvent,kAEDisplayNotice,typeAEList,&displayList)) != noErr)
  296.         return err;
  297.     
  298.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  299.         return err;
  300.     
  301.     if ((err = AECountItems(&displayList,&displayCount)) != noErr)
  302.         return err;
  303.  
  304.     for (displayIndex = 1; displayIndex<=displayCount; displayIndex++)
  305.         {
  306.         DebugStr((StringPtr) "\pProcessing a display");
  307.         
  308.         //    Gather up all the old and new display rectangles into an oldDeskRegion and a newDeskRegion
  309.         }
  310.  
  311.     //    run through all windows calling keeping all windows which were onscreen in the oldDeskRegion
  312.     //    onscreen in the new world. We should really be calling a method to do this so that windows
  313.     //    can individually deal with things in a manner which they can override.
  314.  
  315.  
  316.     return errAEEventNotHandled;    //    we really don’t handle this yet...
  317.     }
  318.  
  319. #if    qInlineInputAware
  320.  
  321. pascal OSErr
  322. HandleTextServicesUpdateActiveInputArea(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  323.     {
  324.     DebugStr("\pTextServicesUpdateActiveInputArea");
  325.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  326.     }
  327.  
  328. pascal OSErr
  329. HandleTextServicesPos2Offset(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  330.     {
  331.     DebugStr("\pTextServicesPos2Offset");
  332.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  333.     }
  334.     
  335. pascal OSErr
  336. HandleTextServicesOffset2Pos(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  337.     {
  338.     DebugStr("\pTextServicesOffset2Pos");
  339.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  340.     }
  341.  
  342. #endif
  343.